/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xgithubclient.core.db.entity;

import android.os.Parcel;
import android.os.Parcelable;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import java.util.Date;

/**
 * @author xuexiang
 * @since 2019-07-05 00:56
 */
@DatabaseTable(tableName = "auth_user")
public class AuthUser implements Parcelable {

    @DatabaseField(generatedId = true)
    private long Id;
    @DatabaseField
    private String accessToken;
    @DatabaseField
    private Date authTime;
    @DatabaseField
    private int expireIn;
    @DatabaseField
    private String scope;
    @DatabaseField
    private boolean selected;
    @DatabaseField
    private String loginId;
    @DatabaseField
    private String name;
    @DatabaseField
    private String avatar;

    public AuthUser() {
    }

    public AuthUser(String accessToken) {
        this.accessToken = accessToken;
    }

    public AuthUser(String accessToken, Date authTime, int expireIn, String scope, boolean selected, String loginId, String name, String avatar) {
        this.accessToken = accessToken;
        this.authTime = authTime;
        this.expireIn = expireIn;
        this.scope = scope;
        this.selected = selected;
        this.loginId = loginId;
        this.name = name;
        this.avatar = avatar;
    }

    public long getId() {
        return Id;
    }

    public AuthUser setId(long id) {
        Id = id;
        return this;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public AuthUser setAccessToken(String accessToken) {
        this.accessToken = accessToken;
        return this;
    }

    public Date getAuthTime() {
        return authTime;
    }

    public AuthUser setAuthTime(Date authTime) {
        this.authTime = authTime;
        return this;
    }

    public int getExpireIn() {
        return expireIn;
    }

    public AuthUser setExpireIn(int expireIn) {
        this.expireIn = expireIn;
        return this;
    }

    public String getScope() {
        return scope;
    }

    public AuthUser setScope(String scope) {
        this.scope = scope;
        return this;
    }

    public boolean isSelected() {
        return selected;
    }

    public AuthUser setSelected(boolean selected) {
        this.selected = selected;
        return this;
    }

    public String getLoginId() {
        return loginId;
    }

    public AuthUser setLoginId(String loginId) {
        this.loginId = loginId;
        return this;
    }

    public String getName() {
        return name;
    }

    public AuthUser setName(String name) {
        this.name = name;
        return this;
    }

    public String getAvatar() {
        return avatar;
    }

    public AuthUser setAvatar(String avatar) {
        this.avatar = avatar;
        return this;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.accessToken);
        dest.writeLong(this.authTime != null ? this.authTime.getTime() : -1);
        dest.writeInt(this.expireIn);
        dest.writeString(this.scope);
        dest.writeByte(this.selected ? (byte) 1 : (byte) 0);
        dest.writeString(this.loginId);
        dest.writeString(this.name);
        dest.writeString(this.avatar);
    }

    protected AuthUser(Parcel in) {
        this.accessToken = in.readString();
        long tmpAuthTime = in.readLong();
        this.authTime = tmpAuthTime == -1 ? null : new Date(tmpAuthTime);
        this.expireIn = in.readInt();
        this.scope = in.readString();
        this.selected = in.readByte() != 0;
        this.loginId = in.readString();
        this.name = in.readString();
        this.avatar = in.readString();
    }

    public static final Creator<AuthUser> CREATOR = new Creator<AuthUser>() {
        @Override
        public AuthUser createFromParcel(Parcel source) {
            return new AuthUser(source);
        }

        @Override
        public AuthUser[] newArray(int size) {
            return new AuthUser[size];
        }
    };

    @Override
    public String toString() {
        return "AuthUser{" +
                "Id=" + Id +
                ", accessToken='" + accessToken + '\'' +
                ", authTime=" + authTime +
                ", expireIn=" + expireIn +
                ", scope='" + scope + '\'' +
                ", selected=" + selected +
                ", loginId='" + loginId + '\'' +
                ", name='" + name + '\'' +
                ", avatar='" + avatar + '\'' +
                '}';
    }
}
